package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.exception.CustomException;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {

    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;

    /**
     * 装箱单列表
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        //根据企业id查询
        PackingExample example = new PackingExample();
        //降序排序
        example.setOrderByClause("create_time desc");
        PackingExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        //分页
        PageInfo<Packing> pageInfo = packingService.findByPage(example, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/packing/packing-list";
    }

    /**
     * 校验选中的合同是否可以添加
     * @return
     */
    @RequestMapping("toPacking")
    @ResponseBody
    public Map<String, Object> toPacking(String id) {
            String[] arr = id.split(",");
            Set<String> set = new HashSet<>();
            for (int i = 0; i < arr.length; i++) {
                //根据每个报运单id查询报运单
                Export export = exportService.findById(arr[i]);
                //把每个报运单的装运港、目的港、收货人放到set集合中
                set.add(export.getShipmentPort());
                set.add(export.getDestinationPort());
                set.add(export.getConsignee());
            }
            //如果set集合的大小不等于3，则说明装运港、目的港、收货人都不一致
        Map<String, Object> resultMap = new HashMap<>();
        if (set.size() != 3){
            resultMap.put("message",0);
        }else {
            resultMap.put("message",1);
        }
            return resultMap;
    }


    /**
     * 添加
     * @param packing
     * @return
     */
    @RequestMapping("edit")
    public String edit(Packing packing){
        //设置企业信息
        packing.setCompanyId(getLoginCompanyId());
        packing.setCompanyName(getLoginCompanyName());

        // 设置创建时间、创建人、创建人的部门
        packing.setCreateTime(new Date());
        packing.setCreateBy(getLoginUser().getId());
        packing.setCreateDept(getLoginUser().getDeptId());
        //保存
        packingService.save(packing);
        return "redirect:/cargo/packing/list";
    }

    /**
     * 点击生成装箱单，进入装箱单添加页面
     * @param id
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(String id){
        Packing packing = packingService.PageBean(id);
        request.setAttribute("packing",packing);
        request.setAttribute("id",id);
        return "cargo/packing/packing-toPacking";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id) {
        Packing packing = packingService.findById(id);
        Integer state = packing.getState();
        String message = "";
        if (state == 1){
            message = "0";
        }else {
            packingService.delete(id);
            message = "1";
        }
        return message;
    }

    /**
     * 查看
     */
    @RequestMapping("/view")
    public String view(String id){
        Packing packing = packingService.findById(id);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-view";
    }

}
